package com.sky.config;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class RabbitmqConfig {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        /**
         * (producer—>rabbitmq broker—>exchange)发布消息成功到交换器后会触发回调方法 ConfirmCallback()
         * correlationData:消息唯一标识
         * ack:是否成功(true or false)
         * cause:失败原因
         * 消息投递成功失败都会进行回调
         **/
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack){
                System.out.println("消息发送失败");
            }
        });

        /**
         * (exchange—>queue)交换器发送消息到队列queue失败时，设置退回，触发退回函数 ReturnCallback()
         * 只有消息投递到queue失败才会触发回调
         **/
        rabbitTemplate.setReturnsCallback(returns -> {
            System.out.println("消息发送失败");
        });
    }
}
